package com.jike.jkgame.data;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;

import com.jike.jkgame.JKApp;
import com.jike.jkgame.db.ResourcesDAO;

import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class MiscDataHelper {

	public static final String PRENAME = "miscdata";

	public static class MISCDATA {
		public static final String EnergyRecoverTime = "energy_recover_time";
		public static final String AmmoRecoverTime = "ammo_recover_time";
		public static final String FreeSpeakTimes = "free_speak_times";
		public static final String TreasureComposeTime = "treasure_compose_time";
		public static final String DispalyedEnemies = "dispalyed_enemies";
		public static final String DispalyedExpeditions = "dispalyed_expeditions";
		public static final String DispalyedMessages = "dispalyed_messages";
		public static final String StrengthenNeededMoney = "strengthen_needed_money";
		public static final String EquipDroppedProbability = "equip_dropped_probability";
		public static final String GetTreasureProbability = "get_treasure_probability";
		public static final String DayInvites = "day_invites";
		public static final String DayMessages = "day_messages_to_leave";
		public static final String GeneralsUpper = "generals_upper";
		public static final String WeaponsUpper = "weapons_upper";
		public static final String ArmorsUpper = "armors_upper";
		public static final String MountsUpper = "horsers_upper";
		public static final String TreasuersUpper = "treasure_upper";

		public static final String MaxLevel = "max_level";
	}

	private SharedPreferences mPreferences;
	private Editor mEditor;
	private MiscUpper mUpper;
	private static MiscDataHelper mInstance = null;

	private MiscDataHelper() {
		mPreferences = JKApp.get().getSharedPreferences(PRENAME, 0);
		mEditor = mPreferences.edit();
	}

	public static MiscDataHelper get() {
		if (mInstance == null) {
			mInstance = new MiscDataHelper();
		}

		return mInstance;
	}

	public int getIntValue(String key) {
		return mPreferences.getInt(key, -1);
	}

	public void setIntVaue(String key, int value) {
		mEditor.putInt(key, value);
		mEditor.commit();
	}
	
	private void updateUpper() {
		if (mUpper == null || mUpper.level != UserStatus.get().getLevel()) {
			mUpper = ResourcesDAO.get().getMiscUpper(UserStatus.get().getLevel());
		}
	}
	
	public int getExperienceUpper() {
		updateUpper();
		if (mUpper != null) {
			return mUpper.exp;
		}
		return -1;
	}
	
	public int getEnergyUpper() {
		updateUpper();
		if (mUpper != null) {
			return mUpper.energy;
		}
		return -1;
	}
	
	public int getAmmoUpper() {
		updateUpper();
		if (mUpper != null) {
			return mUpper.ammo;
		}
		return -1;
	}
	
	public int getTeamUpper() {
		updateUpper();
		if (mUpper != null) {
			return mUpper.team;
		}
		return -1;
	}
	
	public int getFriendUpper() {
		updateUpper();
		if (mUpper != null) {
			return mUpper.friend;
		}
		return -1;
	}

	public static int parseDataFile(InputStream in) {

		DataInputStream dis = new DataInputStream(in);
		int count = 0;
		ResourcesDAO dao = ResourcesDAO.get();

		try {
			int type = dis.readInt();

			int myVersion = dao.getDataFileVersion(type);
			int version = dis.readInt();

			if (myVersion >= version) {
				return 0;
			}

			MiscDataHelper helper = MiscDataHelper.get();
			helper.setIntVaue(MISCDATA.EnergyRecoverTime, dis.readInt());
			helper.setIntVaue(MISCDATA.AmmoRecoverTime, dis.readInt());
			helper.setIntVaue(MISCDATA.FreeSpeakTimes, dis.readByte() & 0xff);
			helper.setIntVaue(MISCDATA.TreasureComposeTime, dis.readInt());
			helper.setIntVaue(MISCDATA.DispalyedEnemies, dis.readByte() & 0xff);
			helper.setIntVaue(MISCDATA.DispalyedExpeditions,
					dis.readByte() & 0xff);
			helper.setIntVaue(MISCDATA.DispalyedMessages, dis.readByte() & 0xff);
			helper.setIntVaue(MISCDATA.StrengthenNeededMoney, dis.readInt());
			helper.setIntVaue(MISCDATA.EquipDroppedProbability,
					dis.readByte() & 0xff);
			helper.setIntVaue(MISCDATA.GetTreasureProbability,
					dis.readByte() & 0xff);
			helper.setIntVaue(MISCDATA.DayInvites, dis.readInt());
			helper.setIntVaue(MISCDATA.DayMessages, dis.readInt());
			helper.setIntVaue(MISCDATA.GeneralsUpper, dis.readByte() & 0xff);
			helper.setIntVaue(MISCDATA.WeaponsUpper, dis.readByte() & 0xff);
			helper.setIntVaue(MISCDATA.ArmorsUpper, dis.readByte() & 0xff);
			helper.setIntVaue(MISCDATA.MountsUpper, dis.readByte() & 0xff);
			helper.setIntVaue(MISCDATA.TreasuersUpper, dis.readByte() & 0xff);

			dao.clearMiscUpper();
			int level = dis.readInt();
			MiscUpper upper = new MiscUpper();
			int value;
			for (int i = 1; i <= level; ++i) {
				upper.level = i;
				upper.exp = dis.readInt();
				upper.energy = dis.readInt();
				value = dis.readByte() & 0xff;
				if (value > upper.ammo) {
					upper.ammo = value;
				}
				value = dis.readByte() & 0xff;
				if (value > upper.team) {
					upper.team = value;
				}
				value = dis.readByte() & 0xff;
				if (value > upper.friend) {
					upper.friend = value;
				}
				dao.insertMiscUpper(upper);

				count++;
			}

			dao.updateDataFileVersion(type, version);
		} catch (IOException e) {
			e.printStackTrace();
		}

		return count;
	}

}
